package com.nl.lib_project.view.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ImageSpan
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.github.gzuliyujiang.dialog.CornerRound
import com.github.gzuliyujiang.dialog.DialogConfig
import com.github.gzuliyujiang.dialog.ModalDialog
import com.nl.lib_core.tool.coroutines.FunctionTypeUnit
import com.nl.lib_core.tool.extends.dp2px
import com.nl.lib_core.tool.extends.getColorResources
import com.nl.lib_core.tool.extends.sp2px
import com.nl.lib_project.R
import com.nl.lib_project.entity.Answer

/** 作者: wangfei 创建时间: 2021/12/30 包名: com.nl.lib_project.view.dialog 类名: PromptBaseNoHeadDialog */
open class PromptBaseNoFooterDialog(
    activity: Activity,
    var tvContent: String,
    private var okCallBack: FunctionTypeUnit?
) : ModalDialog(activity) {

  var mContent: TextView? = null
  var answer: Answer? = null

  init {
    mContent?.text = tvContent
    setBackgroundColor(CornerRound.All, 16, context.getColorResources(R.color.color_white))
  }

  override fun createHeaderView(): View? {
    return View.inflate(activity, R.layout.dialog_header_style_4, null)
  }

  override fun createTopLineView(): View? {
    val view = View(activity)
    view.layoutParams =
        LinearLayout.LayoutParams(
            MATCH_PARENT, (0.5 * activity.resources.displayMetrics.density).toInt())
    view.setBackgroundColor(DialogConfig.getDialogColor().topLineColor())
    return view
  }

  override fun createBodyView(): View {
    mContent = TextView(context)
    mContent?.apply {
      this.text = tvContent
      textSize = context.sp2px(12f)
      gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
      setPadding(
          context.dp2px(20f),
          context.dp2px(20f),
          context.dp2px(20f),
          context.dp2px(20f),
      )
      this.setTextColor(context.getColorResources(R.color.color_black))
    }
    return mContent!!
  }

  override fun createFooterView(): View? {
    return null
  }

  @SuppressLint("UseCompatLoadingForDrawables")
  fun setSpannableBackgroundColor(content: String, resId: Int) {
    val spannableString = SpannableStringBuilder()
    spannableString.append(" $content")
    val drawable = context.resources.getDrawable(resId, null)
    drawable.setBounds(
        0, 0, (drawable.intrinsicWidth * 0.8).toInt(), (drawable.intrinsicHeight * 0.8).toInt())
    val imageSpan = ImageSpan(drawable)
    spannableString.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
    mContent?.text = spannableString
  }

  override fun onCancel() {}

  override fun onOk() {
    okCallBack?.invoke()
  }
}
